package design.patterns.structural;

/**
 * 适配器模式
 * 解释：
 * 适配器模式是一种结构型设计模式，
 * 它能使接口不兼容的对象能够相互合作。
 */

/**
 * 情景
 * 为了更好的与外国友人交流，聘请翻译官进行实时翻译。
 * 中文 与 英文 翻译
 */
public class A_Adapter {
    public static void main(String[] args) {
        Speaker chinese_speaker = new Speaker(Language.Chinese);
        Speaker english_speaker = new Speaker(Language.English);
        Adapter adapter = new Adapter(chinese_speaker, english_speaker);

        chinese_speaker.speak("你好");
        adapter.translateChinesetoEnglish();
        english_speaker.speak("hi");
        adapter.translateEnglishtoChinese();
    }
}


interface Language{
    String Chinese="中文";
    String English ="英文";
}
class Speaker{
    private String language;
    private String sentence;

    public Speaker(String language) {
        this.language = language;
    }

    public String speak(String sentence){
        this.sentence = sentence;
        System.out.println(sentence);
        return sentence;
    }

    public String getSentence() {
        return sentence;
    }

    public String getLanguage() {
        return language;
    }
}

interface Translator{
    void translateChinesetoEnglish();
    void translateEnglishtoChinese();
}
class Adapter implements Translator{
    private Speaker chinese_speaker;
    private Speaker english_speaker;

    public Adapter(Speaker chinese_speaker,Speaker english_speaker) {
        if (chinese_speaker.getLanguage().equals(Language.Chinese)){
            this.chinese_speaker = chinese_speaker;
        }
        if (english_speaker.getLanguage().equals(Language.English)){
            this.english_speaker = english_speaker;
        }
        if (this.chinese_speaker==null||english_speaker==null){
            throw new IllegalArgumentException("请保证输入的Speaker的顺序正确，并且当前仅支持中文和英文");
        }
    }

    @Override
    public void translateChinesetoEnglish() {
        System.out.println("将：【"+ chinese_speaker.getSentence()+"】 翻译为 "+ english_speaker.getLanguage());

    }
    @Override
    public void translateEnglishtoChinese() {
        System.out.println("将：【"+ english_speaker.getSentence()+"】 翻译为 "+ chinese_speaker.getLanguage());
    }
}
